package com.dream.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;


public class MubuFragment extends android.app.Fragment implements View.OnClickListener{

    private ImageView mubuOneUp;
    private ImageView mubuOneStop;
    private ImageView mubuOneDown;
    private ImageView[] imageViews1;
    private int[] imageID_selected;
    private int[] imageID_unselect;

    private MainActivity mainActivity;
    private SDKManager sdkManager;

    public MubuFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mubu, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        mubuOneUp = (ImageView) view.findViewById(R.id.mubu_one_up);
        mubuOneStop = (ImageView) view.findViewById(R.id.mubu_one_stop);
        mubuOneDown = (ImageView) view.findViewById(R.id.mubu_one_down);
        imageViews1=new ImageView[]{mubuOneUp,mubuOneStop,mubuOneDown};
        imageID_selected=new int[]{R.drawable.bg_mubu_up_selected,R.drawable.bg_mubu_stop_selected,R.drawable.bg_mubu_down_selected};
        imageID_unselect=new int[]{R.drawable.bg_mubu_up_unselect,R.drawable.bg_mubu_stop_unselect,R.drawable.bg_mubu_down_unselect};
    }

    public void setOnClick(){
        mubuOneUp.setOnClickListener(this);
        mubuOneStop.setOnClickListener(this);
        mubuOneDown.setOnClickListener(this);
    }

    public void sendMuBuKey(int key){
        if (sdkManager.getLandingSate()){
            sdkManager.senKey(DevUIIDConfig.mubu,key);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.mubu_one_up:
                    setImageBg1(0);
                    sendMuBuKey(0);
                    break;
                case R.id.mubu_one_stop:
                    setImageBg1(1);
                    sendMuBuKey(0);
                    break;
                case R.id.mubu_one_down:
                    setImageBg1(2);
                    sendMuBuKey(0);
                    break;
            }
        }

    //true 开 false 关
    public void setImageBg1(int index){
        for (int i = 0; i < imageViews1.length; i++) {
            if (index==i){
                imageViews1[i].setImageResource(imageID_selected[i]);
            }else {
                imageViews1[i].setImageResource(imageID_unselect[i]);
            }
        }
    }

}
